package com.hexacta.hrs.report.managers;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.vectrics.employee.Employee;

import com.hexacta.hrs.report.AbstractReport;
import com.hexacta.hrs.report.dtos.ReporteGeneralDeVacacionesPendeintesDTO;
import com.hexacta.hrs.services.employee.EmployeeService;
import com.hexacta.hrs.services.timeoff.AvailableLicenseDaysService;
import com.hexacta.hrs.timeoff.AvailableLicenseDays;

/**
 * Implementa el Reporte General de Vacaciones.
 * 
 * @author mfunes
 * 
 */
public class ReporteGeneralDeVacaciones extends AbstractReport {

    private EmployeeService employeeService;

    private AvailableLicenseDaysService availableLicenseDaysService;

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public AvailableLicenseDaysService getAvailableLicenseDaysService() {
        return availableLicenseDaysService;
    }

    public void setAvailableLicenseDaysService(final AvailableLicenseDaysService availableLicenseDaysService) {
        this.availableLicenseDaysService = availableLicenseDaysService;
    }

    /** {@inheritDoc} */
    public ByteArrayOutputStream createPDFReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createPDF(reportTemplateLocation, parameters, getReportData(filters));
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.Report#createXLSReport(java.lang.String,
     * java.lang.String)
     */
    public ByteArrayOutputStream createXLSReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createExcel(reportTemplateLocation, parameters, getReportData(filters));
    }

    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    protected Collection getReportData(final Map<String, Object> filters) {
        Collection<ReporteGeneralDeVacacionesPendeintesDTO> collection = new ArrayList<ReporteGeneralDeVacacionesPendeintesDTO>();

        Collection<Employee> collectionEmployee = employeeService.getAll();
        collectionEmployee.removeAll(employeeService.getNoLongerEmployees());

        for (Employee employee : collectionEmployee) {
            AvailableLicenseDays availableLicenseDays = availableLicenseDaysService.getAvailableLicenseDays(employee);
            ReporteGeneralDeVacacionesPendeintesDTO dto = new ReporteGeneralDeVacacionesPendeintesDTO();
            // if (availableLicenseDays.getVacationsDays() != 0) {
            dto.setFullName(employee.getPerson().getLastName() + ", " + employee.getPerson().getFirstName());
            dto.setCurrentPeriodVacationDays(availableLicenseDays.getCurrentPeriodVacationDays());
            dto.setPreviousPeriodsVacationDays(availableLicenseDays.getPreviousPeriodsVacationDays());
            dto.setTotalDays(availableLicenseDays.getVacationsDays());
            collection.add(dto);
            // }
        }
        if (collection.size() == 0)
            throw new RuntimeException("No data Available for Report");

        Collections.sort((List) collection);
        return collection;
    }

    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    protected Map getReportParameters() {
        // TODO Auto-generated method stub
        return null;
    }

}
